"""
@File         : route_depend.py
@Time         : 2024/05/08 16:38:59
@Author       : Qrj
@Email        : 923207736@qq.com
@Description  : 路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetTenantIdFromAccessTokenDependencies,
    GetUserIdFromAccessTokenDependencies,
)
from src.business.admin.system.station_message.record.schemas.out import (
    StationMessageRecordCompleteOutSchema,
)
from src.business.admin.system.station_message.record.schemas.validate import (
    StationMessageRecordDeleteValidateSchema,
    StationMessageRecordListSearchValidateSchema,
    StationMessageRecordSelfUpdateReadStatusValidateSchema,
)
from src.business.admin.system.station_message.record.service import (
    StationMessageRecordService,
)
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)


async def route_depend_get_station_message_record(
    station_message_record_id: Annotated[
        int, Annotated[int, Query(..., ge=1, description="站内信记录ID")]
    ],
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> StationMessageRecordCompleteOutSchema:
    """查询单个站内信记录的路由依赖

    Args:
        station_message_record_id (Annotated[int, Depends): 站内信记录ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        StationMessageRecordCompleteOutSchema: 站内信记录信息
    """
    station_message_record: StationMessageRecordCompleteOutSchema = (
        await StationMessageRecordService.get_station_message_record_use_id(
            db_session, tenant_id, station_message_record_id
        )
    )
    return station_message_record


async def route_depend_list_station_message_record(
    search_schema: StationMessageRecordListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[StationMessageRecordCompleteOutSchema], int]:
    """查询站内信记录列表的路由依赖

    Args:
        search_schema (Annotated[StationMessageRecordListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[StationMessageRecordNoContentOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[StationMessageRecordCompleteOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await StationMessageRecordService.list_search_station_message_record(
        db_session, tenant_id, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_list_get_user_self_station_message_record(
    search_schema: StationMessageRecordListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    self_id: Annotated[int, Depends(GetUserIdFromAccessTokenDependencies.from_header)],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[StationMessageRecordCompleteOutSchema], int]:
    """查询自身站内信记录列表的路由依赖

    Args:
        search_schema (Annotated[StationMessageRecordListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        tenant_id: 当前token指向的租户ID
        self_id: 当前用户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[StationMessageRecordNoContentOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[StationMessageRecordCompleteOutSchema]
    search_total: int
    (
        search_pagination_result,
        search_total,
    ) = await StationMessageRecordService.list_get_user_self_station_message_record(
        db_session, tenant_id, self_id, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_get_user_self_unread_station_message_record_count(
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    self_id: Annotated[int, Depends(GetUserIdFromAccessTokenDependencies.from_header)],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """查询当前用户未读的站内信记录数量的路由依赖

    Args:
        tenant_id (Annotated[ int, Depends): 租户ID
        self_id (Annotated[int, Depends): 当前用户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        int: 查询总数
    """
    total = await StationMessageRecordService.get_user_self_unread_station_message_record_count(
        db_session, tenant_id, self_id
    )
    return total


async def route_depend_update_self_station_message_record_read_status(
    update_schema: StationMessageRecordSelfUpdateReadStatusValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    self_id: Annotated[int, Depends(GetUserIdFromAccessTokenDependencies.from_header)],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改用户自身站内信记录的已读状态的路由依赖

    Args:
        update_schema (StationMessageRecordSelfUpdateReadStatusValidateSchema): 修改所需的参数
        tenant_id (Annotated[ int, Depends): 租户ID
        self_id (Annotated[int, Depends): 当前用户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await StationMessageRecordService.update_self_station_message_record_read_status(
        db_session, tenant_id, self_id, update_schema
    )


async def route_depend_delete_station_message_record(
    delete_schema: StationMessageRecordDeleteValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除站内信记录的路由依赖

    Args:
        delete_schema (StationMessageRecordDeleteValidateSchema): 删除所需的参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await StationMessageRecordService.delete_station_message_record(
        db_session, tenant_id, delete_schema
    )
